package com.cykj.pos.service.impl;

import com.cykj.common.annotation.DataSource;
import com.cykj.common.enums.DataSourceType;
import com.cykj.pos.domain.dto.OrderDTO;
import com.cykj.pos.profit.dto.AddressDTO;
import com.cykj.pos.profit.dto.BizMerchAddressDTO;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import com.cykj.pos.mapper.BizMerchAddressMapper;
import com.cykj.pos.domain.BizMerchAddress;
import com.cykj.pos.service.IBizMerchAddressService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * 伙伴地址Service业务层处理
 *
 * @author weijianbo
 * @date 2021-04-28
 */
@Service
public class BizMerchAddressServiceImpl extends ServiceImpl<BizMerchAddressMapper, BizMerchAddress> implements IBizMerchAddressService {


    @Resource
    private BizMerchAddressMapper bizMerchAddressMapper;

    @Override
    public List<BizMerchAddress> queryList(BizMerchAddress bizMerchAddress) {
        LambdaQueryWrapper<BizMerchAddress> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(bizMerchAddress.getMerchId())){
            lqw.eq(BizMerchAddress::getMerchId ,bizMerchAddress.getMerchId());
        }
        if (StringUtils.isNotBlank(bizMerchAddress.getAddr())){
            lqw.eq(BizMerchAddress::getAddr ,bizMerchAddress.getAddr());
        }
        if (StringUtils.isNotBlank(bizMerchAddress.getPhone())){
            lqw.eq(BizMerchAddress::getPhone ,bizMerchAddress.getPhone());
        }
        if (StringUtils.isNotBlank(bizMerchAddress.getType())){
            lqw.eq(BizMerchAddress::getType ,bizMerchAddress.getType());
        }
        if (StringUtils.isNotBlank(bizMerchAddress.getCreatTime())){
            lqw.eq(BizMerchAddress::getCreatTime ,bizMerchAddress.getCreatTime());
        }
        if (StringUtils.isNotBlank(bizMerchAddress.getName())){
            lqw.like(BizMerchAddress::getName ,bizMerchAddress.getName());
        }
        if (StringUtils.isNotBlank(bizMerchAddress.getUserId())){
            lqw.eq(BizMerchAddress::getUserId ,bizMerchAddress.getUserId());
        }
        if (StringUtils.isNotBlank(bizMerchAddress.getVar1())){
            lqw.eq(BizMerchAddress::getVar1 ,bizMerchAddress.getVar1());
        }
        if (StringUtils.isNotBlank(bizMerchAddress.getVar2())){
            lqw.eq(BizMerchAddress::getVar2 ,bizMerchAddress.getVar2());
        }
        if (StringUtils.isNotBlank(bizMerchAddress.getVar3())){
            lqw.eq(BizMerchAddress::getVar3 ,bizMerchAddress.getVar3());
        }
        if (StringUtils.isNotBlank(bizMerchAddress.getVar4())){
            lqw.eq(BizMerchAddress::getVar4 ,bizMerchAddress.getVar4());
        }
        if (StringUtils.isNotBlank(bizMerchAddress.getVar5())){
            lqw.eq(BizMerchAddress::getVar5 ,bizMerchAddress.getVar5());
        }
        return this.list(lqw);
    }

    @Override
    @Transactional
    public BizMerchAddress updateByAddrId(BizMerchAddress bizMerchAddress) {
        LocalDateTime localDate = LocalDateTime.now();
        String formatedDate = localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        bizMerchAddress.setUpdateTime(formatedDate);
        int i= bizMerchAddressMapper.updateByAddrId(bizMerchAddress);
        if(i != 0){
            BizMerchAddress address=  bizMerchAddressMapper.selectListAddress(bizMerchAddress);
            return address;
        }
        return null;
    }

    @Override
    public void delete(BizMerchAddress bizMerchAddress) {
        bizMerchAddressMapper.deleteByAddressId(bizMerchAddress);
    }

    @Override
    public BizMerchAddress selectById(BizMerchAddress bizMerchAddress) {
        BizMerchAddress address= bizMerchAddressMapper.selectListAddress(bizMerchAddress);
        return address;
    }

    @Override
    public List<BizMerchAddress> List(BizMerchAddress bizMerchAddress) {
        List<BizMerchAddress> address=bizMerchAddressMapper.getListByUserId(bizMerchAddress);
        return address;
    }

    @Override
    public void updateDefaultAddr(BizMerchAddress bizMerchAddress) {
        List<BizMerchAddress> address=bizMerchAddressMapper.getListByUserId(bizMerchAddress);
        for (BizMerchAddress type:address){
            if(type.getType().equals("1")){
                type.setType("2");
                bizMerchAddressMapper.updateType(type);
            }
        }
        bizMerchAddress.setType("1");
        bizMerchAddressMapper.updateType(bizMerchAddress);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public BizMerchAddressDTO queryAddressListByOrder(Long userId) {
        BizMerchAddress bizMerchAddress = new BizMerchAddress();
        bizMerchAddress.setUserId(userId.toString());
        List<BizMerchAddressDTO> list = bizMerchAddressMapper.queryAddressListByOrder(bizMerchAddress);
        BizMerchAddressDTO bizMerchAddressDTO = new BizMerchAddressDTO();
        if (list.size()!=0){// list不为空说明有地址
            // 查看是否有默认地址 返回默认地址 没有默认地址 返回第一个地址
            for (int i=0;i<list.size();i++){
                if (list.get(i).getType().equals("1")){// 默认地址
                    list.get(i).setIsAddress(0);
                    return list.get(i);
                }
            }
            // 说明没有默认地址
            list.get(0).setIsAddress(0);
            return list.get(0);// 返回地址中第一个
        }else{
            bizMerchAddressDTO.setIsAddress(1);// 地址为空
        }
        return bizMerchAddressDTO;
    }

    @Override
    public AddressDTO getOrderAddress(OrderDTO orderDTO) {
        BizMerchAddress bizMerchAddress = bizMerchAddressMapper.getOrderAddress(orderDTO.getOrderId());
        AddressDTO address = new AddressDTO();
        if (bizMerchAddress==null){

        }else {
            if (bizMerchAddress.getAddr()==null && bizMerchAddress.getAddrInfo()==null){
                System.out.println("地址为空");
            }
            address.setAddress(bizMerchAddress.getAddr()+bizMerchAddress.getAddrInfo());// 详细地址
            if (bizMerchAddress.getPhone()==null){
                address.setPhone("");
            }
            address.setPhone(bizMerchAddress.getPhone());// 电话
            if (bizMerchAddress.getName()==null){
                address.setName("");
            }
            address.setName(bizMerchAddress.getName());// 姓名
        }
        return address;
    }
}
